
package com.hlkj.pay.app.merchant.ext.impl.alipayZft;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Repository;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.AntMerchantExpandIndirectZftSimplecreateRequest;
import com.alipay.api.request.AntMerchantExpandIndirectZftorderQueryRequest;
import com.alipay.api.response.AntMerchantExpandIndirectZftSimplecreateResponse;
import com.alipay.api.response.AntMerchantExpandIndirectZftorderQueryResponse;
import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.merchant.ext.ChannelMerchantAccessExtPt;
import com.hlkj.pay.app.merchant.ext.MerchantExtConstant;
import com.hlkj.pay.app.merchant.ext.dto.access.*;
import com.hlkj.pay.app.merchant.ext.impl.alipay.dto.AlipayConfig;
import com.hlkj.pay.app.merchant.ext.impl.alipay.handler.AliPayCommonParamsHandle;
import com.hlkj.pay.app.merchant.ext.impl.alipayZft.dto.AliPayZftChannelParams;
import com.hlkj.pay.app.merchant.ext.impl.fuyou.utils.FuYouCallBackParamsParseUtil;
import com.hlkj.pay.dto.merchant.AccessChannelParamsDto;
import com.hlkj.pay.dto.merchant.MerchantAccessDto;
import com.hlkj.pay.dto.merchant.MerchantAccessSignLink;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessInfoDO;
import com.hlkj.pay.infrastructure.model.pay.ProviderChannelConfigDO;
import com.hlkj.pay.vo.order.resp.MerchantProductQueryResp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 支付宝直付通进件操作
 * 
 * @author HlpayTeam
 * @date 2024/09/26 10:25
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = MerchantExtConstant.CHANNEL_ALI_PAY_ZFT)
public class AlipayZftMerchantAccessExtPt implements ChannelMerchantAccessExtPt {

    private final AliPayCommonParamsHandle aliPayCommonParamsHandle;

    @Override
    public void channelConfigCheck(ChannelMerchantAccessCommonContext channelMerchantAccessCommonContext) {
        CommonResult<AlipayConfig> alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(channelMerchantAccessCommonContext.getProviderChannelConfig(),
                channelMerchantAccessCommonContext.isTest());
        if (!alipayConfigCommonResult.isSuccess()) {
            channelMerchantAccessCommonContext.setRemark(alipayConfigCommonResult.getMsg());
            channelMerchantAccessCommonContext.setProcessStatus(false);
        }
    }

    @Override
    public void merchantAccess(ChannelMerchantAccessContext channelMerchantAccessContext) {
        log.info("支付宝直付通渠道提交进件 请求参数:{}", JsonUtils.toJsonString(channelMerchantAccessContext));
        CommonResult<AlipayConfig> alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(channelMerchantAccessContext.getProviderChannelConfig(),
                channelMerchantAccessContext.isTest());
        if (!alipayConfigCommonResult.isSuccess()) {
            channelMerchantAccessContext.setRemark(alipayConfigCommonResult.getMsg());
            channelMerchantAccessContext.setProcessStatus(false);
            return;
        }
        MerchantAccessDto merchantAccessDto = channelMerchantAccessContext.getMerchantAccessDto();
        MerchantAccessInfoDO merchantAccessInfo = merchantAccessDto.getMerchantAccessInfo();
        String reqId = IdUtil.fastSimpleUUID();
        // 企业类型
        MerchantEnum.MERCHANT_TYPE merchantType = MerchantEnum.MERCHANT_TYPE.from(merchantAccessInfo.getType());
        try {
            // 初始化支付宝直付通配置
            AlipayConfig alipayConfig = alipayConfigCommonResult.getData();
            // 获取AlipayClient
            AlipayClient client = aliPayCommonParamsHandle.initClient(alipayConfig);

            AntMerchantExpandIndirectZftSimplecreateRequest request = new AntMerchantExpandIndirectZftSimplecreateRequest();

            AntMerchantExpandIndirectZftSimplecreateModel model = new AntMerchantExpandIndirectZftSimplecreateModel();
            model.setExternalId(reqId);
            model.setAliasName(merchantAccessDto.getMerchantAccessBusiness().getShortName());
            // 联系人
            ContactInfo contactInfo = new ContactInfo();
            // 判断法人是否是联系人
            if(CommonEnum.YES_NO_TYPE.YES.getCode().equals(merchantAccessInfo.getContactType())){
                contactInfo.setName(merchantAccessDto.getMerchantAccessLegal().getName());
                contactInfo.setMobile(merchantAccessDto.getMerchantAccessLegal().getPhone());
                contactInfo.setIdCardNo(merchantAccessDto.getMerchantAccessLegal().getCertNo());
            }else{
                contactInfo.setName(merchantAccessDto.getMerchantAccessContact().getName());
                contactInfo.setMobile(merchantAccessDto.getMerchantAccessContact().getPhone());
                contactInfo.setIdCardNo(merchantAccessDto.getMerchantAccessContact().getCertNo());
            }
            model.setContactInfos(contactInfo);
            // 结算规则
            DefaultSettleRule settleRule = new DefaultSettleRule();
            settleRule.setDefaultSettleType("alipayAccount");
            settleRule.setDefaultSettleTarget(merchantAccessDto.getMerchantAccessCard().getCardNo());
            model.setDefaultSettleRule(settleRule);

            model.setService(CollUtil.newArrayList("当面付"));
            // 商户类别码
            String mcc = merchantAccessDto.getMerchantAccessBusiness().getMcc();
            if(StrUtil.isEmpty(mcc)){
                channelMerchantAccessContext.setRemark("支付宝直付通商户类别必填");
                channelMerchantAccessContext.setProcessStatus(false);
                return;
            }
            model.setMcc(mcc.split("_")[1]);
            model.setInDoorImages(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessBusiness().getStoreIndoorUrl(),alipayConfig));
            model.setOutDoorImages(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessBusiness().getStoreHeaderUrl(),alipayConfig));

            // 地址信息
            AddressInfo addressInfo = new AddressInfo();
            addressInfo.setProvinceCode(merchantAccessDto.getMerchantAccessBusiness().getProvinceCode());
            addressInfo.setCityCode(merchantAccessDto.getMerchantAccessBusiness().getCityCode());
            addressInfo.setDistrictCode(merchantAccessDto.getMerchantAccessBusiness().getAreaCode());
            addressInfo.setAddress(merchantAccessDto.getMerchantAccessBusiness().getAddress());
            model.setBusinessAddress(addressInfo);

            // 设置结算支付宝账号
            model.setAlipayLogonId(merchantAccessDto.getMerchantAccessLegal().getOtherValue());
            model.setBindingAlipayLogonId(merchantAccessDto.getMerchantAccessLegal().getOtherValue());
            model.setServicePhone(merchantAccessDto.getMerchantAccessBusiness().getServicePhone());

            if(merchantType == MerchantEnum.MERCHANT_TYPE.COMPANY){
                model.setName(merchantAccessDto.getMerchantAccessLicense().getName());
            }else{
                model.setName(merchantAccessDto.getMerchantAccessLegal().getName());
            }

            // 账号类型 0：对公账户 1：对私法人账户 2：对私非法人账户
            MerchantEnum.CARD_TYPE cardType = MerchantEnum.CARD_TYPE.from(merchantAccessDto.getMerchantAccessCard().getType());
            switch (cardType) {
                case CORPORATE_ACCOUNT:
                    break;
                case PRIVATE_CORPORATE_ACCOUNT:
                    break;
                case PRIVATE_NON_CORPORATE_ACCOUNT:
                    // 经营场景类的其他的文件 第一个文件是否上传(业务办理授权函)
                    List<String> fileUrls = merchantAccessDto.getMerchantAccessBusiness().getFileUrls();
                    if(CollUtil.isEmpty(fileUrls)){
                        // 直接结束
                        channelMerchantAccessContext.setRemark("业务办理授权函未上传");
                        channelMerchantAccessContext.setProcessStatus(false);
                        return;
                    }
                    model.setLicenseAuthLetterImage(aliPayCommonParamsHandle.zftUploadImg(fileUrls.get(0),alipayConfig));
                    break;
                default:
            }
            // model.setTradeScene(CollUtil.newArrayList("OFFLINE"));

            request.setBizModel(model);
            log.info("支付宝直付通提交进件 请求参数:{}", JsonUtils.toJsonString(model));
            AntMerchantExpandIndirectZftSimplecreateResponse createResponse = client.execute(request);
            log.info("支付宝直付通提交进件 响应参数:{}", JsonUtils.toJsonString(createResponse));
            if(createResponse.isSuccess()){
                // 商户号 用openId
                merchantAccessInfo.setApplyNo(createResponse.getOrderId());
                // 结算卡号
                merchantAccessInfo.setExtendA(merchantAccessDto.getMerchantAccessCard().getCardNo());
                // 下一步就是查询申请状态
                channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                channelMerchantAccessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMIT);
            }else{
                channelMerchantAccessContext.setProcessStatus(false);
                channelMerchantAccessContext.setRemark(createResponse.getSubMsg());
                channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
            }
        }
        catch (Exception e) {
            channelMerchantAccessContext.setProcessStatus(false);
            channelMerchantAccessContext.setRemark(e.getMessage());
            channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

    @Override
    public String requestUrl(MerchantAccessDto merchantAccessInfoDO) {
        return null;
    }

    @Override
    public String channelParams(AccessChannelParamsDto accessChannelParamsDto) {

        log.info("直付通 同步channelParams：{}", JSONUtil.toJsonStr(accessChannelParamsDto));
        AliPayZftChannelParams params = new AliPayZftChannelParams();
        params.setSubMchId(accessChannelParamsDto.getSubMchId());
        params.setSettleNo(accessChannelParamsDto.getExtendA());
        try{
            // 绑定分账账号
            if(StrUtil.isNotEmpty(accessChannelParamsDto.getMchChannelCode())){
                CommonResult<AlipayConfig> alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(accessChannelParamsDto.getProviderChannelConfigDO(),
                        true);
                if (alipayConfigCommonResult.isSuccess()) {
                    log.info("直付通 同步channelParams 绑定默认分账账号：{}",JSONUtil.toJsonStr(alipayConfigCommonResult.getData()));
                    aliPayCommonParamsHandle.alipayZftBindProtAccount(accessChannelParamsDto.getMchChannelCode(),alipayConfigCommonResult.getData());
                }
            }
        }catch (Exception e){
            log.error("channelParams alipayZftBindProtAccount fail:{}",e);
        }
        return JsonUtils.toJsonStringNotNull(params);
    }

    @Override
    public void merchantAccessQuery(ChannelMerchantAccessQueryContext channelMerchantAccessQueryContext) {

        CommonResult<AlipayConfig> alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(channelMerchantAccessQueryContext.getProviderChannelConfig(),
                channelMerchantAccessQueryContext.isTest());
        if (!alipayConfigCommonResult.isSuccess()) {
            channelMerchantAccessQueryContext.setRemark(alipayConfigCommonResult.getMsg());
            channelMerchantAccessQueryContext.setProcessStatus(false);
            return;
        }
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessQueryContext.getMerchantAccessInfo();
        try{
            // 初始化支付宝直付通配置
            AlipayConfig alipayConfig = alipayConfigCommonResult.getData();
            // 获取AlipayClient
            AlipayClient client = aliPayCommonParamsHandle.initClient(alipayConfig);
            // 构造请求参数以调用接口
            AntMerchantExpandIndirectZftorderQueryRequest request = new AntMerchantExpandIndirectZftorderQueryRequest();
            AntMerchantExpandIndirectZftorderQueryModel model = new AntMerchantExpandIndirectZftorderQueryModel();
            // 设置申请单id
            model.setOrderId(merchantAccessInfo.getApplyNo());
            request.setBizModel(model);
            log.info("支付宝直付通进件查询 请求参数:{}", JsonUtils.toJsonString(request));
            AntMerchantExpandIndirectZftorderQueryResponse response = client.execute(request);
            log.info("支付宝直付通进件查询 返回参数:{}", JsonUtils.toJsonString(response));

            if(response.isSuccess()){
                Optional<ZftSubMerchantOrder> optional = response.getOrders().stream().filter(order->order.getOrderId().equals(merchantAccessInfo.getApplyNo())).findFirst();
                if(optional.isPresent()){
                    ZftSubMerchantOrder zftSubMerchantOrder = optional.get();
                    List<String> errMsg = new ArrayList<>();
                    if(StrUtil.isNotEmpty(zftSubMerchantOrder.getReason())){
                        errMsg.add("进件审批失败:"+zftSubMerchantOrder.getReason());
                    }
                    if(StrUtil.isNotEmpty(zftSubMerchantOrder.getKzAuditMemo())){
                        errMsg.add("客资审批失败:"+zftSubMerchantOrder.getKzAuditMemo());
                    }
                    if(StrUtil.isNotEmpty(zftSubMerchantOrder.getFkAuditMemo())){
                        errMsg.add("风控审批失败:"+zftSubMerchantOrder.getFkAuditMemo());
                    }
                    switch (zftSubMerchantOrder.getStatus()) {
                        case "-1":
                            channelMerchantAccessQueryContext.setProcessStatus(false);
                            channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                            if(CollUtil.isNotEmpty(errMsg)){
                                channelMerchantAccessQueryContext.setRemark(String.join(",",errMsg));
                            }else{
                                channelMerchantAccessQueryContext.setRemark(response.getSubMsg());
                            }
                            break;
                        case "031":
                            // 二级商户确认
                            String subConfirm = zftSubMerchantOrder.getSubConfirm();
                            if(StrUtil.isNotEmpty(subConfirm)){
                                switch (subConfirm){
                                    case "NOT_CONFIRM":
                                    case "CREATE":
                                        MerchantAccessSignLink merchantAccessSignLink = new MerchantAccessSignLink();
                                        merchantAccessSignLink.setLink(zftSubMerchantOrder.getSubSignQrCodeUrl());
                                        merchantAccessSignLink.setName("商户确认签约链接");
                                        channelMerchantAccessQueryContext.setSignLinks(CollUtil.newArrayList(merchantAccessSignLink));
                                        channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGNING);
                                        break;
                                    case "FAIL":
                                        channelMerchantAccessQueryContext.setProcessStatus(false);
                                        channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                                        if(CollUtil.isNotEmpty(errMsg)){
                                            channelMerchantAccessQueryContext.setRemark(String.join(",",errMsg));
                                        }else{
                                            channelMerchantAccessQueryContext.setRemark(response.getSubMsg());
                                        }
                                        break;
                                    case "SKIP":
                                    case "FINISH":
                                        merchantAccessInfo.setSubMchId(zftSubMerchantOrder.getSmid());
                                        channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
                                        channelMerchantAccessQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.FINISH);
                                        break;
                                }
                            }
                            break;
                        case "99":
                            merchantAccessInfo.setSubMchId(zftSubMerchantOrder.getSmid());
                            channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
                            channelMerchantAccessQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.FINISH);
                            break;
                    }
                }
            }else{
                channelMerchantAccessQueryContext.setProcessStatus(false);
                channelMerchantAccessQueryContext.setRemark(response.getSubMsg());
            }
        }catch (Exception e){
            log.error("支付宝直付通进件查询请求失败,失败信息 message: {}", e);
            channelMerchantAccessQueryContext.setProcessStatus(false);
            channelMerchantAccessQueryContext.setRemark(e.getMessage());
        }

    }

    @Override
    public void merchantSign(ChannelMerchantAccessSignContext channelMerchantAccessSignContext) {
    }

    @Override
    public void merchantSignQuery(ChannelMerchantAccessSignContext channelMerchantAccessSignContext) {
        CommonResult<AlipayConfig> alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(channelMerchantAccessSignContext.getProviderChannelConfig(),
                channelMerchantAccessSignContext.isTest());
        if (!alipayConfigCommonResult.isSuccess()) {
            channelMerchantAccessSignContext.setRemark(alipayConfigCommonResult.getMsg());
            channelMerchantAccessSignContext.setProcessStatus(false);
            return;
        }
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessSignContext.getMerchantAccessInfoDO();
        try{
            // 初始化支付宝直付通配置
            AlipayConfig alipayConfig = alipayConfigCommonResult.getData();
            // 获取AlipayClient
            AlipayClient client = aliPayCommonParamsHandle.initClient(alipayConfig);
            // 构造请求参数以调用接口
            AntMerchantExpandIndirectZftorderQueryRequest request = new AntMerchantExpandIndirectZftorderQueryRequest();
            AntMerchantExpandIndirectZftorderQueryModel model = new AntMerchantExpandIndirectZftorderQueryModel();
            // 设置申请单id
            model.setOrderId(merchantAccessInfo.getApplyNo());
            request.setBizModel(model);
            log.info("支付宝直付通进件签约查询 请求参数:{}", JsonUtils.toJsonString(request));
            AntMerchantExpandIndirectZftorderQueryResponse response = client.execute(request);
            log.info("支付宝直付通进件签约查询 返回参数:{}", JsonUtils.toJsonString(response));

            if(response.isSuccess()){
                Optional<ZftSubMerchantOrder> optional = response.getOrders().stream().filter(order->order.getOrderId().equals(merchantAccessInfo.getApplyNo())).findFirst();
                if(optional.isPresent()){
                    ZftSubMerchantOrder zftSubMerchantOrder = optional.get();
                    List<String> errMsg = new ArrayList<>();
                    if(StrUtil.isNotEmpty(zftSubMerchantOrder.getReason())){
                        errMsg.add("进件审批失败:"+zftSubMerchantOrder.getReason());
                    }
                    if(StrUtil.isNotEmpty(zftSubMerchantOrder.getKzAuditMemo())){
                        errMsg.add("客资审批失败:"+zftSubMerchantOrder.getKzAuditMemo());
                    }
                    if(StrUtil.isNotEmpty(zftSubMerchantOrder.getFkAuditMemo())){
                        errMsg.add("风控审批失败:"+zftSubMerchantOrder.getFkAuditMemo());
                    }
                    switch (zftSubMerchantOrder.getStatus()) {
                        case "-1":
                            channelMerchantAccessSignContext.setProcessStatus(false);
                            channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                            if(CollUtil.isNotEmpty(errMsg)){
                                channelMerchantAccessSignContext.setRemark(String.join(",",errMsg));
                            }else{
                                channelMerchantAccessSignContext.setRemark(response.getSubMsg());
                            }
                            break;
                        case "031":
                            // 二级商户确认
                            String subConfirm = zftSubMerchantOrder.getSubConfirm();
                            if(StrUtil.isNotEmpty(subConfirm)){
                                switch (subConfirm){
                                    case "NOT_CONFIRM":
                                    case "CREATE":
                                        break;
                                    case "FAIL":
                                        channelMerchantAccessSignContext.setProcessStatus(false);
                                        channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                                        if(CollUtil.isNotEmpty(errMsg)){
                                            channelMerchantAccessSignContext.setRemark(String.join(",",errMsg));
                                        }else{
                                            channelMerchantAccessSignContext.setRemark(response.getSubMsg());
                                        }
                                        break;
                                    case "SKIP":
                                    case "FINISH":
                                        merchantAccessInfo.setSubMchId(zftSubMerchantOrder.getSmid());
                                        channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
                                        channelMerchantAccessSignContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.FINISH);
                                        break;
                                }
                            }
                            break;
                        case "99":
                            merchantAccessInfo.setSubMchId(zftSubMerchantOrder.getSmid());
                            channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
                            channelMerchantAccessSignContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.FINISH);
                            break;
                    }
                }
            }else{
                channelMerchantAccessSignContext.setProcessStatus(false);
                channelMerchantAccessSignContext.setRemark(response.getSubMsg());
            }
        }catch (Exception e){
            log.error("支付宝直付通进件签约查询请求失败,失败信息 message: {}", e);
            channelMerchantAccessSignContext.setProcessStatus(false);
            channelMerchantAccessSignContext.setRemark(e.getMessage());
        }
    }

    @Override
    public void merchantBusinessOpen(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

    }

    @Override
    public void wechatConfigInit(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
    }

    @Override
    public void wechatConfigAdd(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
    }

    @Override
    public void wechatConfigQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {

    }

    @Override
    public void wechatAuthConfig(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        // 微信参数配置
    }

    @Override
    public boolean authConfigQueryType() {
        return true;
    }

    @Override
    public void wechatAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        // 文件上传完成就是微信参数自动配置
    }

    @Override
    public void alipayAuthConfig(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
    }

    @Override
    public void alipayAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
    }

    @Override
    public MerchantAccessCallBackDto accessCallBackParamsParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        MerchantAccessCallBackDto merchantAccessCallBackDto=new MerchantAccessCallBackDto();
        merchantAccessCallBackDto.setRequestId(parameterMap.get("external_agreement_no")[0]);
        merchantAccessCallBackDto.setSubMchId(parameterMap.get("agreement_no")[0]);
        merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SET);
        return merchantAccessCallBackDto;
    }

    @Override
    public CommonResult<String> accessCallbackParse(String callBackBody, List<ProviderChannelConfigDO> channelConfigDOList) {
        return null;
    }

    @Override
    public MerchantBusinessCallBackDto businessCallBackParamsParse(String callBackBody, ProviderChannelConfigDO providerChannelConfigDO) {
        return FuYouCallBackParamsParseUtil.businessCallBackParamsParse(callBackBody);
    }

    @Override
    public boolean verify(String callBackBody, ProviderChannelConfigDO providerChannelConfigDO) {
        return true;
    }

    @Override
    public String callBackResponse(CommonResult commonResult) {
        if (commonResult.isSuccess()) {
            return "SUCCESS";
        }
        else {
            return "FAIL";
        }
    }

    @Override
    public void termAdd(ChannelMerchantTermContext channelMerchantTermContext) {

    }

    @Override
    public void reloadMerchantAuth(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

    }

    @Override
    public void channelSplit(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

    }

    @Override
    public void productUpdate(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

    }

    @Override
    public Integer queryMerAuthStatus(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        return null;
    }

    @Override
    public MerchantProductQueryResp productQuery(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        return null;
    }

    @Override
    public MerchantChannelSplitCallBackDto easySplitCallBackParamsParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {
        return null;
    }


    private CommonResult<AntMerchantExpandIndirectZftCreateModel> create(String reqId,MerchantAccessDto merchantAccessDto,AlipayConfig alipayConfig){
        MerchantAccessInfoDO merchantAccessInfo = merchantAccessDto.getMerchantAccessInfo();

        AntMerchantExpandIndirectZftCreateModel model = new AntMerchantExpandIndirectZftCreateModel();
        // 企业类型
        MerchantEnum.MERCHANT_TYPE merchantType = MerchantEnum.MERCHANT_TYPE.from(merchantAccessInfo.getType());

        model.setExternalId(reqId);
        model.setMerchantType(aliPayCommonParamsHandle.companyType(Integer.valueOf(merchantAccessDto.getMerchantAccessLicense().getCompanyType()),merchantType));
        if(merchantType == MerchantEnum.MERCHANT_TYPE.COMPANY){
            model.setName(merchantAccessDto.getMerchantAccessLicense().getName());
            model.setCertImage(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessLegal().getCertFrontUrl(),alipayConfig));
            model.setCertImageBack(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessLegal().getCertBackUrl(),alipayConfig));
            model.setCertType(aliPayCommonParamsHandle.licenseType(merchantAccessDto.getMerchantAccessLicense().getLicenseType()));
            model.setCertNo(merchantAccessDto.getMerchantAccessLicense().getLicenseCode());
            model.setCertName(merchantAccessDto.getMerchantAccessLicense().getName());
        }else{
            model.setCertType("100");
        }
        model.setAliasName(merchantAccessDto.getMerchantAccessBusiness().getShortName());
        model.setLegalName(merchantAccessDto.getMerchantAccessLegal().getName());
        model.setLegalCertNo(merchantAccessDto.getMerchantAccessLegal().getCertNo());
        model.setLegalCertType("100");
        // 商户类别码
        String mcc = merchantAccessDto.getMerchantAccessBusiness().getMcc();
        if(StrUtil.isEmpty(mcc)){
            return CommonResult.error(0,"支付宝直付通商户类别必填");
        }
        model.setMcc(mcc.split("_")[1]);
        model.setService(CollUtil.newArrayList("当面付","订单码支付"));
        model.setInDoorImages(CollUtil.newArrayList(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessBusiness().getStoreIndoorUrl(),alipayConfig)));
        model.setOutDoorImages(CollUtil.newArrayList(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessBusiness().getStoreHeaderUrl(),alipayConfig)));

        // 地址信息
        AddressInfo addressInfo = new AddressInfo();
        addressInfo.setProvinceCode(merchantAccessDto.getMerchantAccessBusiness().getProvinceCode());
        addressInfo.setCityCode(merchantAccessDto.getMerchantAccessBusiness().getCityCode());
        addressInfo.setDistrictCode(merchantAccessDto.getMerchantAccessBusiness().getAreaCode());
        addressInfo.setAddress(merchantAccessDto.getMerchantAccessBusiness().getAddress());
        model.setBusinessAddress(addressInfo);

        model.setAlipayLogonId(merchantAccessDto.getMerchantAccessLegal().getOtherValue());
        model.setLegalCertFrontImage(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessLegal().getCertFrontUrl(),alipayConfig));
        model.setLegalCertBackImage(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessLegal().getCertBackUrl(),alipayConfig));
        model.setServicePhone(merchantAccessDto.getMerchantAccessBusiness().getServicePhone());

        // 联系人
        ContactInfo contactInfo = new ContactInfo();
        // 判断法人是否是联系人
        if(CommonEnum.YES_NO_TYPE.YES.getCode().equals(merchantAccessInfo.getContactType())){
            contactInfo.setName(merchantAccessDto.getMerchantAccessLegal().getName());
            contactInfo.setMobile(merchantAccessDto.getMerchantAccessLegal().getPhone());
        }else{
            contactInfo.setName(merchantAccessDto.getMerchantAccessContact().getName());
            contactInfo.setMobile(merchantAccessDto.getMerchantAccessContact().getPhone());
        }
        model.setContactInfos(CollUtil.newArrayList(contactInfo));

        // 账号类型 0：对公账户 1：对私法人账户 2：对私非法人账户
        MerchantEnum.CARD_TYPE cardType = MerchantEnum.CARD_TYPE.from(merchantAccessDto.getMerchantAccessCard().getType());
        switch (cardType) {
            case CORPORATE_ACCOUNT:
                break;
            case PRIVATE_CORPORATE_ACCOUNT:
                break;
            case PRIVATE_NON_CORPORATE_ACCOUNT:
                // 经营场景类的其他的文件 第一个文件是否上传(业务办理授权函)
                List<String> fileUrls = merchantAccessDto.getMerchantAccessBusiness().getFileUrls();
                if(CollUtil.isEmpty(fileUrls)){
                    // 直接结束
                    return CommonResult.error(0,"业务办理授权函未上传");
                }
                model.setLicenseAuthLetterImage(aliPayCommonParamsHandle.zftUploadImg(fileUrls.get(0),alipayConfig));
                break;
            default:
        }

        model.setAlipayLogonId(merchantAccessDto.getMerchantAccessLegal().getOtherValue());
        // 结算规则
        DefaultSettleRule settleRule = new DefaultSettleRule();
        settleRule.setDefaultSettleType("alipayAccount");
        settleRule.setDefaultSettleTarget(merchantAccessDto.getMerchantAccessCard().getCardNo());
        model.setDefaultSettleRule(settleRule);
        return CommonResult.success(model);
    }


    private CommonResult<AntMerchantExpandIndirectZftModifyModel> update(String reqId,MerchantAccessDto merchantAccessDto,AlipayConfig alipayConfig){
        MerchantAccessInfoDO merchantAccessInfo = merchantAccessDto.getMerchantAccessInfo();

        AntMerchantExpandIndirectZftModifyModel model = new AntMerchantExpandIndirectZftModifyModel();
        // 企业类型
        MerchantEnum.MERCHANT_TYPE merchantType = MerchantEnum.MERCHANT_TYPE.from(merchantAccessInfo.getType());

        model.setExternalId(reqId);
        model.setMerchantType(aliPayCommonParamsHandle.companyType(Integer.valueOf(merchantAccessDto.getMerchantAccessLicense().getCompanyType()),merchantType));
        if(merchantType == MerchantEnum.MERCHANT_TYPE.COMPANY){
            model.setName(merchantAccessDto.getMerchantAccessLicense().getName());
            model.setCertImage(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessLegal().getCertFrontUrl(),alipayConfig));
            model.setCertImageBack(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessLegal().getCertBackUrl(),alipayConfig));
            model.setCertType(aliPayCommonParamsHandle.licenseType(merchantAccessDto.getMerchantAccessLicense().getLicenseType()));
        }else{
            model.setCertType("100");
        }
        model.setAliasName(merchantAccessDto.getMerchantAccessBusiness().getShortName());
        model.setLegalName(merchantAccessDto.getMerchantAccessLegal().getName());
        model.setLegalCertNo(merchantAccessDto.getMerchantAccessLegal().getCertNo());
        model.setLegalCertType("100");
        // 商户类别码
        String mcc = merchantAccessDto.getMerchantAccessBusiness().getMcc();
        if(StrUtil.isEmpty(mcc)){
            return CommonResult.error(0,"支付宝直付通商户类别必填");
        }
        model.setMcc(mcc.split("_")[1]);
        model.setService(CollUtil.newArrayList("当面付","订单码支付"));
        model.setInDoorImages(CollUtil.newArrayList(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessBusiness().getStoreIndoorUrl(),alipayConfig)));
        model.setOutDoorImages(CollUtil.newArrayList(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessBusiness().getStoreHeaderUrl(),alipayConfig)));

        // 地址信息
        AddressInfo addressInfo = new AddressInfo();
        addressInfo.setProvinceCode(merchantAccessDto.getMerchantAccessBusiness().getProvinceCode());
        addressInfo.setCityCode(merchantAccessDto.getMerchantAccessBusiness().getCityCode());
        addressInfo.setDistrictCode(merchantAccessDto.getMerchantAccessBusiness().getAreaCode());
        addressInfo.setAddress(merchantAccessDto.getMerchantAccessBusiness().getAddress());
        model.setBusinessAddress(addressInfo);

        model.setAlipayLogonId(merchantAccessDto.getMerchantAccessLegal().getOtherValue());
        model.setLegalCertFrontImage(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessLegal().getCertFrontUrl(),alipayConfig));
        model.setLegalCertBackImage(aliPayCommonParamsHandle.zftUploadImg(merchantAccessDto.getMerchantAccessLegal().getCertBackUrl(),alipayConfig));
        model.setServicePhone(merchantAccessDto.getMerchantAccessBusiness().getServicePhone());

        // 联系人
        ContactInfo contactInfo = new ContactInfo();
        // 判断法人是否是联系人
        if(CommonEnum.YES_NO_TYPE.YES.getCode().equals(merchantAccessInfo.getContactType())){
            contactInfo.setName(merchantAccessDto.getMerchantAccessLegal().getName());
            contactInfo.setMobile(merchantAccessDto.getMerchantAccessLegal().getPhone());
        }else{
            contactInfo.setName(merchantAccessDto.getMerchantAccessContact().getName());
            contactInfo.setMobile(merchantAccessDto.getMerchantAccessContact().getPhone());
        }
        model.setContactInfos(CollUtil.newArrayList(contactInfo));

        // 账号类型 0：对公账户 1：对私法人账户 2：对私非法人账户
        MerchantEnum.CARD_TYPE cardType = MerchantEnum.CARD_TYPE.from(merchantAccessDto.getMerchantAccessCard().getType());
        switch (cardType) {
            case CORPORATE_ACCOUNT:
                break;
            case PRIVATE_CORPORATE_ACCOUNT:
                break;
            case PRIVATE_NON_CORPORATE_ACCOUNT:
                // 经营场景类的其他的文件 第一个文件是否上传(业务办理授权函)
                List<String> fileUrls = merchantAccessDto.getMerchantAccessBusiness().getFileUrls();
                if(CollUtil.isEmpty(fileUrls)){
                    // 直接结束
                    return CommonResult.error(0,"业务办理授权函未上传");
                }
                model.setLicenseAuthLetterImage(aliPayCommonParamsHandle.zftUploadImg(fileUrls.get(0),alipayConfig));
                break;
            default:
        }

        model.setAlipayLogonId(merchantAccessDto.getMerchantAccessLegal().getOtherValue());
        // 结算规则
        DefaultSettleRule settleRule = new DefaultSettleRule();
        settleRule.setDefaultSettleType("alipayAccount");
        settleRule.setDefaultSettleTarget(merchantAccessDto.getMerchantAccessCard().getCardNo());
        model.setDefaultSettleRule(settleRule);
        return CommonResult.success(model);
    }


}
